<?php

namespace App\Repository;

use App\Models\Image;
use App\Models\ImageType;

class ImageRepo extends BaseRepo
{

    public function getQuery($param)
    {
        $query = Image::query()->where("is_delete", 0);
        $query->select(["id","code","url","type_id","is_recommend","create_time","update_time"]);
        $query->with("type:id,name");
        $query->where("is_used",0);

        if(!empty($param['type_id'])) {
            $query->where("type_id",$param['type_id']);
        }

        if(!empty($param['is_recommend']) && $param['is_recommend'] == 1) {
            $query->where("is_recommend",$param['is_recommend']);
        }

        if(!empty($param['access_mid'])) {
            $query->where("access_mid",$param['access_mid']);
        }

        return $query->orderByDesc('id');
    }

    public function getTypeQuery($param): \Illuminate\Database\Eloquent\Builder
    {
        $query = ImageType::query()->where("is_delete",0);
        $query->select(["id","name"]);
        return $query->orderByDesc('id');
    }


    public function getById($id)
    {
        return Image::query()->find($id);
    }

    public function setUsed($id,$updated): int
    {
        $updated['is_used'] = 1;
        return Image::query()->where("id", $id)->where("is_used", 0)->update($updated);
    }

    /**
     * 根据藏品ID更新信息
     * @param array $update
     * @param int $special_id
     * @return int
     * @author shen.zk
     * @date 2023-11-20 16:26
     */
    public function saveBySpecialId(array $update,int $special_id)
    {
        return Image::query()->where("special_id", $special_id)->update($update);
    }
}
